package clids.ex1.analysis;

import clids.ex1.data_structures.*;
import java.io.*;

public class testSuite {

	public static boolean testFile(String path) {

		String line = "";

		String[] split_line = null;

		int line_num = 0;

		try {

			File f = new File(path);

			BufferedReader in = new BufferedReader(new FileReader(f));

			//reads 1st line - comment - ignore.
			line = in.readLine();
			line_num++;

			//reads 2nd line - constructor.
			line = in.readLine();
			line_num++;

			MyHashSet hs = null;

			if (line.startsWith("N:")) {

				split_line = line.split(" +");
				int capacity = Integer.parseInt(split_line[1]);
				double up_fac = Double.parseDouble(split_line[2]);
				double low_fac = Double.parseDouble(split_line[3]);
				hs = new MyHashSet(capacity, up_fac, low_fac);

			} else {

				if (line.startsWith("A:")) {

					split_line = line.split(" +");
					String[] data = new String[split_line.length - 1];

					for (int i = 1; i < split_line.length; i++) {
						data[i - 1] = split_line[i];
					}

					hs = new MyHashSet(data);

				} else {
					hs = new MyHashSet();
				}
			}

			line = in.readLine();
			line_num++;

			while (line != null) { //we read until we get to EOF.

				split_line = line.split(" +");

				if (split_line[0].equals("add")) {
					boolean ret = hs.add(split_line[1]);

					if (!split_line[2].equals("" + ret)) {
						System.out.println("error in file: " + path
								+ " , in line: " + line_num);
						return false;
					}
				}

				if (split_line[0].equals("delete")) {
					boolean ret = hs.delete(split_line[1]);

					if (!split_line[2].equals("" + ret)) {
						System.out.println("error in file: " + path
								+ " , in line: " + line_num);
						return false;
					}
				}

				if (split_line[0].equals("contains")) {
					boolean ret = hs.contains(split_line[1]);

					if (!split_line[2].equals("" + ret)) {
						System.out.println("error in file: " + path
								+ " , in line: " + line_num);
						return false;
					}
				}

				if (split_line[0].equals("size")) {
					int ret = hs.size();

					if (!split_line[1].equals("" + ret)) {
						System.out.println("error in file: " + path
								+ " , in line: " + line_num);
						return false;
					}
				}

				if (split_line[0].equals("capacity")) {
					int ret = hs.capacity();

					if (!split_line[1].equals("" + ret)) {
						System.out.println("error in file: " + path
								+ " , in line: " + line_num);
						return false;
					}
				}

				//reads next line.
				line = in.readLine();
				line_num++;

			} //end of while.

			in.close();

		} catch (IOException e) {

			System.out.println(e);
		}

		return true;

	}

	public static void main(String[] args) {

		String folder = "C:\\test\\";

		for (int i = 1; i <= 32; i++) {

			String path = folder + "0" + ((i < 10) ? ("0" + i) : ("" + i))
					+ ".txt";

			boolean ret = testFile(path);

			if (!ret) {
				System.exit(0);
			}

		}

		System.out.println("Success!!!");

	} //end of main.

} //end of class.
